package com.icss.ah.vehicleUsers.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.vehicleUsers.bean.CarUserBean;
import com.icss.j2ee.util.UUID;

@Service
@Transactional
public class VehicleUsersService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;

	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	public List<CarUserBean> queryVehicleUsersList(HashMap map) {
		List<CarUserBean> list = new ArrayList<CarUserBean>();
		try{
			//根据登录账号获取账号基本信息
			list = sqlSessionTemplate.selectList("VehicleUsersBean.queryVehicleUsersList", map);
			infolog.info("VehicleUsersService|queryVehicleUsersList|list:"+list);
			list = list == null ? new ArrayList<CarUserBean>() : list;
		} catch (Exception e) {
			e.printStackTrace();
			list = new ArrayList<CarUserBean>();
			errlog.error("VehicleUsersService|queryVehicleUsersList", e);
		}
		return list;
	}
	
	public Long queryVehicleUsersCount(HashMap map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne("VehicleUsersBean.queryVehicleUsersCount", map);
			infolog.info("VehicleUsersService|queryVehicleUsersCount|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("VehicleUsersService|queryVehicleUsersCount", e);
		} 
		return reValue;
	}
	
	public List queryUsersList(HashMap map) {
		List list = new ArrayList();
		try{
			//根据登录账号获取账号基本信息
			list = sqlSessionTemplate.selectList("VehicleUsersBean.queryUsersList", map);
			infolog.info("VehicleUsersService|queryUsersList|list:"+list);
			list = list == null ? new ArrayList() : list;
		} catch (Exception e) {
			e.printStackTrace();
			list = new ArrayList();
			errlog.error("VehicleUsersService|queryUsersList", e);
		}
		return list;
	}
	
	public List queryUsersDetail(HashMap map) {
		List list = new ArrayList();
		try{
			//根据登录账号获取账号基本信息
			list = sqlSessionTemplate.selectList("VehicleUsersBean.queryUsersDetail", map);
			infolog.info("VehicleUsersService|queryUsersDetail|list:"+list);
			list = list == null ? new ArrayList() : list;
		} catch (Exception e) {
			e.printStackTrace();
			list = new ArrayList();
			errlog.error("VehicleUsersService|queryUsersDetail", e);
		}
		return list;
	}
	
	public int deleteRelation(HashMap map) {
		int reValue = 0;
		try {
			reValue = sqlSessionTemplate.delete("VehicleUsersBean.deleteRelation", map);
			infolog.info("VehicleUsersService|deleteRelation|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("VehicleUsersService|deleteRelation", e);
		} 
		return reValue;
	}
	
	public boolean saveRelation(HashMap map){
		boolean reValue = false;
		try {
			map.put("uuid",new UUID().toString() );
			sqlSessionTemplate.insert("VehicleUsersBean.insertRelation", map);
			reValue = true;
			infolog.info("VehicleUsersService|saveRelation|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("VehicleUsersService|saveRelation", e);
		} 
		return reValue;
	}
}
